package com.xsh.controller;

import com.github.pagehelper.PageInfo;
import com.xsh.pojo.Result;
import com.xsh.pojo.User;
import com.xsh.pojo.UserVo;
import com.xsh.service.RoleService;
import com.xsh.service.UserService;
import com.xsh.utils.GetClassUtils;
import com.xsh.utils.R;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@CrossOrigin("http://localhost:8082")
@RequestMapping("/user")
public class UserController {

    private UserService userService = (UserService) GetClassUtils.getClass("userServiceImpl");

    private RoleService roleService = (RoleService) GetClassUtils.getClass("roleServiceImpl");

    @PostMapping("/login")
    @ResponseBody
    public Result login(@RequestBody UserVo user, HttpSession session){
        String validateCode = (String) session.getAttribute("captcha");
        if (!user.getCaptcha().equals(validateCode)) {
            return new Result("验证码错误", 1);
        }
        Subject subject = SecurityUtils.getSubject();
        if (user.getRememberMe() != null&&user.getRememberMe().trim().length() != 0) {
            subject.login(new UsernamePasswordToken(user.getUsername(), user.getPassword(), true));
        } else {
            subject.login(new UsernamePasswordToken(user.getUsername(), user.getPassword()));
        }
        System.out.println("登录成功");
        return new Result(0);
    }

    @GetMapping("/info")
    @ResponseBody
    public R info(){
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipal();
        User user = userService.findByUsername(username);
        return R.ok().put("user", user);
    }

    @GetMapping("/info/{userId}")
    @ResponseBody
    public R userInfo(@PathVariable("userId") Integer userId){
        User user = userService.findByUserId(userId);
        List<Integer> roles = roleService.findByUserId(userId);
        user.setRoles(roles);
        return R.ok().put("user", user);
    }

    @PostMapping("/update")
    @ResponseBody
    public R update(@RequestBody User user){
        userService.updateUser(user);
        return R.ok();
    }

    @PostMapping("/del/{userId}")
    @ResponseBody
    public R delete(@PathVariable Integer userId){
        userService.deleteUser(userId);
        return R.ok();
    }

    @RequestMapping("/list")
    @ResponseBody
    public R list(String order, Integer limit, Integer offset){

        PageInfo<User> userList = userService.findByPageInfo(order, limit, offset);
        return R.ok().put("total", userList.getTotal()).put("rows", userList.getList());
    }

    @PostMapping("/save")
    @ResponseBody
    public R save(@RequestBody User user){
        if (user.getUsername() == null||user.getUsername().trim().length() == 0) {
            return R.error("用户名不能为空");
        }
        if (user.getPassword() == null||user.getPassword().trim().length() == 0) {
            return R.error("密码不能为空");
        }
        User user1 = userService.findByUsername(user.getUsername());
        if (user1 != null) {
            return R.error("用户名已存在");
        }
        userService.regist(user);
        System.out.println(1);
        return R.ok();
    }

    @GetMapping("/logout")
    @ResponseBody
    public Result logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return new Result(0);
    }

}
